package org.example.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.constant.RConst;
import org.example.ymcc.domain.Course;
import org.example.ymcc.domain.CourseSummary;
import org.example.ymcc.domain.CourseUserLearn;
import org.example.ymcc.mapper.CourseMapper;
import org.example.ymcc.mapper.CourseSummaryMapper;
import org.example.ymcc.mapper.CourseUserLearnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "CourseConsumer",
        topic = MQConstants.ALI_PAY_CALLBACK_ORDER_TOPIC,
        selectorExpression = MQConstants.ALI_PAY_CALLBACK_ORDER_TAG
)
public class CourseConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseUserLearnMapper courseUserLearnMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseSummaryMapper courseSummaryMapper;

    /**
     * 修改订单状态
     * @param messageExt
     */
    @Transactional
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, Constants.UTF8);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String orderSn = jsonObject.get("orderSn").toString();
            String courseIds = jsonObject.get("courseId").toString();
            String userId = jsonObject.get("userId").toString();

            String[] split = courseIds.split(",");
            for (String courseId : split) {
                CourseUserLearn courseUserLearn = new CourseUserLearn();
                courseUserLearn.setCreateTime(new Date());
                courseUserLearn.setCourseId(Long.valueOf(courseId));
                courseUserLearn.setCourseOrderNo(orderSn);
                courseUserLearn.setLoginId(Long.valueOf(userId));
                courseUserLearn.setState(RConst.Num.INT_0);
                Course course = courseMapper.selectById(Long.valueOf(courseId));
                courseUserLearn.setStartTime(course.getStartTime());
                courseUserLearn.setEndTime(course.getEndTime());
                courseUserLearnMapper.insert(courseUserLearn);
                //销量加1
                CourseSummary courseSummary = courseSummaryMapper.selectById(Long.valueOf(courseId));
                courseSummary.setSaleCount(courseSummary.getSaleCount()+RConst.Num.INTEGER_1);
                courseSummaryMapper.updateById(courseSummary);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
